package com.badshun.ojs;

import java.text.NumberFormat;

public class OJSUtils {
    public static boolean hasChar(String str, char token) {
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == token) {
                return true;
            }
        }

        return false;
    }

    public static boolean has(String str, String tokens) {
        return str.equals(str.replace(tokens, ""));
    }

    public static String getBracketContent(String content, String bracketType) {
        String contentInBracket = "";
        boolean startRecord = false;

        for (int i = 0; i < content.length(); i++) {
            if (startRecord && content.charAt(i) != bracketType.charAt(0)
                    && content.charAt(i) != bracketType.charAt(1)) {
                contentInBracket += content.charAt(i);
            }

            if (content.charAt(i) == bracketType.charAt(0)) {
                startRecord = true;
                contentInBracket = "";
            } else if (content.charAt(i) == bracketType.charAt(1)) {
                return contentInBracket;
            }
        }

        return contentInBracket;
    }

    public static boolean isMathOperator(char token) {
        return (token == '+') || (token == '-') || (token == '*') || (token == '/');
    }

    public static boolean isDigital(String str) {
        try {
            Float.valueOf(str);

            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static String calc(String numberA, String numberB, char operator) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);

        switch (operator) {
            case '+':
                return nf.format(Double.valueOf(numberA) + Double.valueOf(numberB));
            case '-':
                return nf.format(Double.valueOf(numberA) - Double.valueOf(numberB));
            case '*':
                return nf.format(Double.valueOf(numberA) * Double.valueOf(numberB));
            case '/':
                return nf.format(Double.valueOf(numberA) / Double.valueOf(numberB));
            default:
                return "calc_no_result";
        }
    }

    public static int calcBool(String tokenA, String tokenB, String operator, OJSVar.VAR_TYPE varType) {
        switch (operator) {
            case ">":
                return (Double.valueOf(tokenA) > Double.valueOf(tokenB)) ? 1 : 0;
            case ">=":
                return (Double.valueOf(tokenA) >= Double.valueOf(tokenB)) ? 1 : 0;
            case "<":
                return (Double.valueOf(tokenA) < Double.valueOf(tokenB)) ? 1 : 0;
            case "<=":
                return (Double.valueOf(tokenA) <= Double.valueOf(tokenB)) ? 1 : 0;
            case "!=":
                switch (varType) {
                    case Int:
                        return (Integer.valueOf(tokenA) != Integer.valueOf(tokenB)) ? 1 : 0;
                    case Float:
                        return (Double.valueOf(tokenA) != Double.valueOf(tokenB)) ? 1 : 0;
                    case Bool:
                        return (Boolean.valueOf(tokenA) != Boolean.valueOf(tokenB)) ? 1 : 0;
                    default:
                        return -1;
                }
            case "==":
                switch (varType) {
                    case Int:
                        return (Integer.valueOf(tokenA) == Integer.valueOf(tokenB)) ? 1 : 0;
                    case Float:
                        return (Double.valueOf(tokenA) == Double.valueOf(tokenB)) ? 1 : 0;
                    case Bool:
                        return (Boolean.valueOf(tokenA) == Boolean.valueOf(tokenB)) ? 1 : 0;
                    default:
                        return -1;
                }
            case "&&":
                return (Boolean.valueOf(tokenA) && Boolean.valueOf(tokenB)) ? 1 : 0;
            case "||":
                return (Boolean.valueOf(tokenA) || Boolean.valueOf(tokenB)) ? 1 : 0;
            default:
                return -1;
        }
    }
}
